<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Api\Controller;
use \think\Log;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends WorksController {

	//系统首页
    public function index(){
        redirect(U('Works/lookApplyList'));
    }
    
    
    /*
     * 首页导航控制器
     */
    public function header(){
        
        $channel = M('channel');
        $header_info = $channel->order('sort desc,id')->where(['status'=>1])->select();
        $tree = $this->arrToTree($header_info,$pid=0);
	$this->ajaxReturn($this->successReturn($tree));
    }
    
    //导航递归函数
    private function arrToTree($data,$pid=0){  
        $tree = array();  
        foreach($data as $k => $v){  
            if($v['pid'] == $pid){
                $son = $this->arrToTree($data,$v['id']);
                    $v['class'] = $son; 
                    if (empty($v['class'])){
                        unset($v['class']);
                    }
                    $tree[] = $v;
            }  
        }          
        return $tree;  
    }  
    
    /*
     * 媒体站提供首图
     * 
     */
    public function getFirstImg(){

        $p = I('get.p',1);
        $r = I('get.r',15);
        $Model = M('listitems');
        $img_arr = array();
        $map['img'] = array('NEQ','');
        $map['s_url'] = array('NEQ','');
        $result = $Model->field('id,img,s_url,width,height')->where($map)->page($p,$r)->select();
        foreach ($result as $my_pirture){
            // $check = img_exists($my_pirture['s_url']);
            // if ($check){
            //     $img_arr[$my_pirture['id']]['s_url'] = $my_pirture['s_url'];                    
            //     $img_arr[$my_pirture['id']]['width'] = $my_pirture['width'];                    
            //     $img_arr[$my_pirture['id']]['height'] = $my_pirture['height'];                    
            // }else{
            //     $tietu_result = tietu($my_pirture['img']);
            //     if ($tietu_result){
            //         if ($this->recordTietu('items',$tietu_result,$my_pirture['id']) != FALSE){
            //             $img_arr[$my_pirture['id']]['s_url'] = $tietu_result['s_url'];                    
            //             $img_arr[$my_pirture['id']]['width'] = $tietu_result['width'];                    
            //             $img_arr[$my_pirture['id']]['height'] = $tietu_result['height']; 
            //         }
            //     }
            // }
            $img_arr[$my_pirture['id']]['s_url'] = $my_pirture['s_url'];                    
            $img_arr[$my_pirture['id']]['width'] = $my_pirture['width'];                    
            $img_arr[$my_pirture['id']]['height'] = $my_pirture['height']; 
        }
        $this->ajaxReturn($img_arr);
    }
    
    //后置方法
//    public function _after_getFirstImg(){
//        
//        foreach ($this->data as $my_pirture){
//
//            if (empty($my_pirture['s_url'])){
//                $tietu_result = tietu($my_pirture['image']);
//                $r = $this->recordTietu('items',$tietu_result,$my_pirture['id']);
//                if ($r === FALSE){
//                    break;
//                }
//            }
//        }
//    }
    
    
    
        /*记录贴图库数据*/
    private function recordTietu($table = 'items',$data = array(),$id=null){

        if (empty($table)){
            $this->error('缺少数据表');
        }
        if (empty($data)){
            $this->error('缺少返回数据');
        }
        if (empty($id)){
            $this->error('缺少id');
        }
                   Log::record('贴图库错误:'.$data['code'],'NOTIC'); 
        //记录错误
        if ($data['code']){
           Log::record('贴图库错误:'.$data['info'],'NOTIC'); 
           return FALSE;
        }else{
            $itmesModel = M($table);
            $itmesModel->s_url = $data['s_url'];
            $itmesModel->t_url = $data['t_url'];
            $itmesModel->width = $data['width'];
            $itmesModel->height = $data['height'];
            $result = $itmesModel->where('id='.$id)->save();
            if ( false === $result ){
                Log::record('贴图库错误:'.$id.'更新失败','NOTIC');
                return FALSE;
            }
        }
    }

    
}
